package com.example.hellowordl; // 这里替换成你的包名

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    // 声明控件变量
    private EditText etUsername;
    private EditText etPassword;
    private CheckBox cbAgree;
    private Button btnLogin;
    private TextView tvRegister;
    private TextView tvPasswordHint; // 密码提示文字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 加载布局文件
        setContentView(R.layout.activity_main);

        // 绑定控件
        initViews();

        // 设置各种交互事件
        setListeners();
    }

    // 绑定控件的方法
    private void initViews() {
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        cbAgree = findViewById(R.id.cbAgree);
        btnLogin = findViewById(R.id.btnLogin);
        tvRegister = findViewById(R.id.tvRegister);
        tvPasswordHint = findViewById(R.id.tvPasswordHint); // 初始化密码提示控件

        // 初始隐藏密码提示
        tvPasswordHint.setVisibility(View.GONE);
    }

    // 设置各种监听器的方法
    private void setListeners() {
        // 1. 用户名输入框处理：自动去除前后空格
        etUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString().trim();
                if (!text.equals(s.toString())) {
                    etUsername.setText(text);
                    etUsername.setSelection(text.length());
                }
                // 检查输入状态，更新登录按钮
                checkInputStatus();
            }
        });

        // 2. 密码输入框监听：实时检查是否为空
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                String password = s.toString().trim();
                // 密码为空时显示提示，否则隐藏
                if (password.isEmpty()) {
                    tvPasswordHint.setVisibility(View.VISIBLE);
                } else {
                    tvPasswordHint.setVisibility(View.GONE);
                }
                // 检查输入状态，更新登录按钮
                checkInputStatus();
            }
        });

        // 3. 协议勾选框：控制登录按钮状态
        cbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            checkInputStatus();
        });

        // 4. 登录按钮点击事件
        btnLogin.setOnClickListener(v -> {
            handleLogin();
        });

        // 5. 注册文字点击事件
        tvRegister.setOnClickListener(v -> {
            Toast.makeText(MainActivity.this, "跳转到注册页", Toast.LENGTH_SHORT).show();
        });
    }

    // 检查输入状态，更新登录按钮可用性
    private void checkInputStatus() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        boolean isAgree = cbAgree.isChecked();

        // 只有当用户名、密码都不为空且勾选协议时，登录按钮才可用
        boolean isEnable = !username.isEmpty() && !password.isEmpty() && isAgree;

        btnLogin.setEnabled(isEnable);
        if (isEnable) {
            btnLogin.setBackgroundTintList(getResources().getColorStateList(android.R.color.holo_blue_light));
        } else {
            btnLogin.setBackgroundTintList(getResources().getColorStateList(android.R.color.darker_gray));
        }
    }

    // 处理登录逻辑的方法
    private void handleLogin() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        // 双重检查，确保输入有效
        if (username.isEmpty()) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (password.isEmpty()) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!cbAgree.isChecked()) {
            Toast.makeText(this, "请同意用户协议", Toast.LENGTH_SHORT).show();
            return;
        }

        // 所有检查通过，显示登录成功
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
    }
}
